﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BO;
using System.Data.SqlClient;
using System.Data;

namespace DAO
{
    public class D_MilkTreatment
    {
        public bool CreateOneMilkTreatment(MilkTreatment_Info objMilkTreatment_Info)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("MilkTreatment_Insert", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"MilkTreatmentID", objMilkTreatment_Info.MilkTreatmentID));
            command.Parameters.Add(new SqlParameter(@"MixtID", objMilkTreatment_Info.MixtID));
            command.Parameters.Add(new SqlParameter(@"TreatmentTypeID", objMilkTreatment_Info.TreatmentTypeID));
            command.Parameters.Add(new SqlParameter(@"Time", objMilkTreatment_Info.Time));
            command.Parameters.Add(new SqlParameter(@"TreatmentLocationID", objMilkTreatment_Info.TreatmentLocationID));
            command.Parameters.Add(new SqlParameter(@"Description", objMilkTreatment_Info.Description));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //**********************************************************************************       
        public bool EditOneMilkTreatment(MilkTreatment_Info objMilkTreatment_Info)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("MilkTreatment_Update", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@MilkTreatmentID", objMilkTreatment_Info.MilkTreatmentID));
            command.Parameters.Add(new SqlParameter("@MixtID", objMilkTreatment_Info.MixtID));
            command.Parameters.Add(new SqlParameter("@TreatmentTypeID", objMilkTreatment_Info.TreatmentTypeID));
            command.Parameters.Add(new SqlParameter("@Time", objMilkTreatment_Info.Time));
            command.Parameters.Add(new SqlParameter("@TreatmentLocationID", objMilkTreatment_Info.TreatmentLocationID));
            command.Parameters.Add(new SqlParameter("@Description", objMilkTreatment_Info.Description));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //******************************************************************************       
        public bool RemoveOneMilkTreatment(int MilkTreatmentID)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("MilkTreatment_Delete", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"MilkTreatmentID", MilkTreatmentID));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //******************************************************************************       
        public DataTable GetAllMilkTreatment()
        {
            DataSet ds = new DataSet();
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("MilkTreatment_SelectAll", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            try
            {
                objConnection.Open();
                SqlDataAdapter da = new SqlDataAdapter(command);
                da.Fill(ds);
            }
            catch (System.Exception ex)
            {
                return null;
            }
            finally
            {
                objConnection.Close();
            }
            return ds.Tables[0];
        }
        //******************************************************************************       
        public MilkTreatment_Info GetOneMilkTreatment(int MilkTreatmentID)
        {
            MilkTreatment_Info objMilkTreatment_Info = new MilkTreatment_Info();
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("MilkTreatment_SelectByID", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"MilkTreatmentID", MilkTreatmentID));
            try
            {
                objConnection.Open();
                SqlDataReader ordMilkTreatment = command.ExecuteReader(); //OjectRecordDatareader
                ordMilkTreatment.Read();
                if (ordMilkTreatment.HasRows)
                {
                    if (ordMilkTreatment["MilkTreatmentID"] != DBNull.Value)
                    {
                        objMilkTreatment_Info.MilkTreatmentID = Convert.ToInt16(ordMilkTreatment["MilkTreatmentID"]);
                    }
                    if (ordMilkTreatment["MixtID"] != DBNull.Value)
                    {
                        objMilkTreatment_Info.MixtID = Convert.ToInt16(ordMilkTreatment["MixtID"]);
                    }
                    if (ordMilkTreatment["TreatmentTypeID"] != DBNull.Value)
                    {
                        objMilkTreatment_Info.TreatmentTypeID = Convert.ToInt16(ordMilkTreatment["TreatmentTypeID"]);
                    }
                    if (ordMilkTreatment["Time"] != DBNull.Value)
                    {
                        objMilkTreatment_Info.Time = Convert.ToDateTime(ordMilkTreatment["Time"]);
                    }
                    if (ordMilkTreatment["TreatmentLocationID"] != DBNull.Value)
                    {
                        objMilkTreatment_Info.TreatmentLocationID = Convert.ToInt16(ordMilkTreatment["TreatmentLocationID"]);
                    }
                    if (ordMilkTreatment["Description"] != DBNull.Value)
                    {
                        objMilkTreatment_Info.Description = Convert.ToString(ordMilkTreatment["Description"]);
                    }
                }
            }
            catch (System.Exception ex)
            {
                return null;
            }
            finally
            {
                objConnection.Close();
            }
            return objMilkTreatment_Info;
        }
    }
}
